<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="ownerName">
        <el-input
          v-model="queryParams.ownerName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="owerPhone">
        <el-input
          v-model="queryParams.owerPhone"
          placeholder="请输入电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="小区ID" prop="communityId">
        <el-input
          v-model="queryParams.communityId"
          placeholder="请输入小区ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="downCommunity">同步平台</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="证书编号" align="center" prop="spNo" />
      <el-table-column label="订单状态" align="center" prop="status">
       
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.status=='审核通过'">{{ scope.row.status }}</el-tag>
          <el-tag type="primary" v-if="scope.row.status=='待审核'">{{ scope.row.status }}</el-tag>
          <el-tag type="warning" v-if="scope.row.status=='待签字'">{{ scope.row.status }}</el-tag>
          <el-tag type="danger" v-if="scope.row.status=='待支付'">{{ scope.row.status }}</el-tag>
          
        </template>
      </el-table-column>
      <el-table-column label="权属人" align="center" prop="ownerName" />
      <el-table-column label="出售人" align="center" prop="originalOwner" />
      <el-table-column label="电话" align="center" prop="owerPhone" />
      <el-table-column label="办证员" align="center" prop="economicId" />
      <el-table-column label="地址" align="center" prop="roomAddress" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="小区" align="center" prop="communityName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:record:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:record:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改备案订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="recordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="电话" prop="owerPhone">
          <el-input v-model="form.owerPhone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="地址" prop="roomAddress">
          <el-input v-model="form.roomAddress" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="小区ID" prop="communityId">
          <el-input v-model="form.communityId" placeholder="请输入小区ID" />
        </el-form-item>
        <el-form-item label="源ID" prop="resourceId">
          <el-input v-model="form.resourceId" placeholder="请输入源ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
     <!-- 添加或修改小区信息对话框 -->
     <el-dialog title="同步平台数据" v-model="jsonOpen" width="900px" append-to-body>
      <el-form  label-width="100px">
        
        <el-form-item label="JSON数据串">
          <el-input type="textarea"  :rows="30" v-model="jsonStr" placeholder="请输入JSON数据串" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitJson">确 定</el-button>
          <el-button @click="jsonOpen=false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Record">
import dayjs from "dayjs";
import { listRecord, getRecord, delRecord, addRecord, updateRecord,pullRecoed } from "@/api/manage/record";
import { listCommunity, pullCommunity } from "@/api/manage/community";
const { proxy } = getCurrentInstance();

const recordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
let jsonOpen=ref(false)
let jsonStr=ref("")
let communityList=ref([])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    ownerName: null,
    owerPhone: null,
    idCard: null,
    roomAddress: null,
    amount: null,
    communityId: null,
    resourceId: null,
  },
  rules: {
  }
});

/** 查询小区信息列表 */
function getCommunityList() {
  
  listCommunity({pageNum: 1,
    pageSize: 20000,}).then(response => {
    communityList.value = response.rows;
    
  });
}

const { queryParams, form, rules } = toRefs(data);

/** 查询备案订单列表 */
function getList() {
  loading.value = true;
  listRecord(queryParams.value).then(response => {
    recordList.value = response.rows.map(item=>{
      let data=communityList.value.filter(fitem=>{
        return fitem.communityId==item.communityId
      })[0]
      console.log(data)
      item.communityName=data?data.name:''
      return item
    })
    total.value = response.total;
    loading.value = false;
  });
}

function subRecoed(){

  let data=JSON.parse(jsonStr.value).data.records.map(item=>{
   
    return {
      spNeighbourhood:item.spNeighbourhood,
      spAddr:item.spAddr,
      ownerName:item.spOwner,
      owerPhone:item.spOwnerPhone,
      idCard:item.spPapersNo,
      roomAddress:item.spOwnerAddr,
      amount:item.amount,
      communityId:'',
      resourceId:item.id,
      originalOwner:item.originalOwner,
      checkTime:item.filingsTime,
      status:item.echoMap.status,
      spNo:item.spNo,
      economicId:item.echoMap.economicId?item.echoMap.economicId:'-',
      remark:item.supplementText,
      createTime:item.createdTime
    }
  })


  for(let i=0;i<data.length;i++){
    for(let j=0;j<communityList.value.length;j++){
      
      if(data[i].spNeighbourhood==communityList.value[j].name&&data[i].spAddr==communityList.value[j].address){
        data[i].communityId=communityList.value[j].communityId
        break;
      }
    }
  }



  pullRecoed(data).then(response => {
    proxy.$modal.msgSuccess("同步数据成功");

    jsonOpen.value = false;
    getList()

    

  });
  
}

function submitJson(){

  if(!jsonStr.value){
    proxy.$modal.msgError("非法数据");
    return
  }

  let data=JSON.parse(jsonStr.value).data.records.map(item=>{
    return {
      name:item.spNeighbourhood,
      province:"江西",
      city:item.nodeName,
      developerName:item.developerName,
      address:item.spAddr,
      longitude:'',
      latitude:'',
      remark:''
    }
  })



  pullCommunity(data).then(response => {
    

    
    getCommunityList()

    setTimeout(function(){
      subRecoed()
    },1000)

  });
}

function downCommunity(){
jsonStr.value=''
jsonOpen.value=true

}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    recordId: null,
    ownerName: null,
    owerPhone: null,
    idCard: null,
    roomAddress: null,
    amount: null,
    communityId: null,
    resourceId: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("recordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.recordId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加备案订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _recordId = row.recordId || ids.value
  getRecord(_recordId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改备案订单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["recordRef"].validate(valid => {
    if (valid) {
      if (form.value.recordId != null) {
        updateRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _recordIds = row.recordId || ids.value;
  proxy.$modal.confirm('是否确认删除备案订单编号为"' + _recordIds + '"的数据项？').then(function() {
    return delRecord(_recordIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/record/export', {
    ...queryParams.value
  }, `record_${new Date().getTime()}.xlsx`)
}
getCommunityList()

setTimeout(function(){
  getList()
},1000)

</script>
